import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)


const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  {
    path: '/test',
    name: 'test',
    component: () => import('@/views/test/test.vue')
  },
  {
    path: '/',
    name: '',
    component: () => import('@/views/login/login.vue')
  },
  {
    path: '/login',
    name: 'login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import('@/views/login/login.vue')
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('@/views/Home/Home.vue')
  },
  {
    path: '/userinfo',
    name: 'userinfo',
    component: () => import('@/views/userInfo/userInfo.vue')
  },
  {
    path: '/roomdevice',
    name: 'roomdevice',
    component: () => import('@/views/RoomDevice/RoomDevice.vue')
  },
  {
    path: '/manage',
    name: 'manage',
    component: () => import('@/views/backManage/index.vue'),
    children:[ //通过children配置子级路由
        {    
          
          path:'/', 
          
          redirect: 'alarmlist'
        },
        {    
          name:'alarmlist',
   				path:'alarmlist', 
   				component:() => import('@/views/backManage/alarmList.vue'),
           meta: { title: '告警列表' }
   			},
   			{    
          name:'rooms',
   				path:'rooms', 
   				component:() => import('@/views/backManage/Rooms.vue'),
           meta: { title: '房间列表' }
   			},
         {    
          name:'roomtype',
   				path:'roomtype', 
   				component:() => import('@/views/backManage/RoomType.vue'),
           meta: { title: '房间类型' }
   			},
         {    
          name:'roomfloor',
   				path:'roomfloor', 
   				component:() => import('@/views/backManage/RoomFloor.vue'),
           meta: { title: '房间区域' }
   			},
         {    
          name:'devices',
   				path:'devices', 
   				component:() => import('@/views/backManage/Devices.vue'),
           meta: { title: '设备列表' }
   			},
         {    
          name:'devicetype',
   				path:'devicetype', 
   				component:() => import('@/views/backManage/DeviceType.vue'),
           meta: { title: '设备类型' }
   			},
         {    
          name:'users',
   				path:'users', 
   				component:() => import('@/views/backManage/UserList.vue'),
           meta: { title: '用户列表' }
   			},
         {    
          name:'loglist',
   				path:'loglist', 
   				component:() => import('@/views/backManage/LogsList.vue'),
           meta: { title: '日志列表' }
   			},
         {    
          name:'logcount',
   				path:'logcount', 
   				component:() => import('@/views/backManage/LogsCount.vue'),
           meta: { title: '日志统计' }
   			},
   			
   		  ]
  },
  
]

// const router = new VueRouter({
//   mode: 'history',
//   base: process.env.BASE_URL,
//   routes
// })

const createRouter = () => new VueRouter({
  // mode: 'history',
  // base: process.env.BASE_URL,
  routes: routes
})
const router = createRouter()
//写一个重置路由的方法，切换用户后，或者退出时清除动态加载的路由
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // 新路由实例matcer，赋值给旧路由实例的matcher，（相当于replaceRouter）
}
const RouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
  return RouterPush.call(this, to).catch(err => err)
}
const RouterReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace (to) {
  return RouterReplace.call(this, to).catch(err => err)
}


export default router
